//
//  DoctorViewController.m
//  QuickCure
//
//  Created by 曹魏 on 16/9/15.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "CWBackViewController.h"
#import "CWMessageModel.h"
#import "MessageItem.h"

#import "DoctorMsgViewController.h"

@interface CWBackViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,weak)UICollectionView *collectionView;
//附加滚动条:
@property (nonatomic,strong)NSArray *tableViewArray;

@property (nonatomic,weak)UIScrollView *navBar;

@property (nonatomic,strong)NSMutableArray *btnArray;

@property (nonatomic,weak)UIView *rollView;

@property (nonatomic,assign)CGFloat scrollViewBeginX;
@property (nonatomic,assign)CGFloat rollViewBeginX;
@property (nonatomic,assign)CGFloat rollViewBeginW;
@property (nonatomic,assign)NSInteger currentPage;

//记录滚动状态:
@property (nonatomic,assign)BOOL isScroll;

@property (nonatomic,assign)BOOL isClick;


//子控制器:
@property (nonatomic,strong)DoctorMsgViewController *dmVC;


//button
@property (nonatomic,weak) UIButton *btn;
@end



@implementation CWBackViewController


- (void)setCurrentPage:(NSInteger)currentPage{
    _currentPage = currentPage;
    [self.btnArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (obj.tag != _currentPage) {
            
            [obj setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            
            
        }
        else{
            [obj setTitleColor:[UIColor colorWithRed:31/255.0 green:194/255.0 blue:194/255.0 alpha:1] forState:UIControlStateNormal];
        }
        
    }];
    
    
    
    
}





- (NSMutableArray *)btnArray
{
    if(!_btnArray)
    {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (instancetype)init
{
    if(self = [super init])
    {
        [self setupUI];
        
        //添加自定义channelLabel:
        [self setupUI2];
        
        
    }
    return  self;
}
- (void)setupUI
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, size.width, size.height -190) collectionViewLayout:flowLayout];
    self.collectionView = collectionV;
    [self.view addSubview:collectionV];
    [collectionV setDataSource:self];
    [collectionV setDelegate:self];
    
    [collectionV setBackgroundColor:[UIColor lightGrayColor]];
    
    [self.view setBackgroundColor:[UIColor greenColor]];
    
    flowLayout.itemSize = self.collectionView.bounds.size;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    
    [self.collectionView registerClass:[MessageItem class] forCellWithReuseIdentifier:@"cell"];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加子控制器:
    DoctorMsgViewController *docMsgVC = [[DoctorMsgViewController alloc]init];
    self.dmVC = docMsgVC;
    
}


#pragma mark
#pragma mark UICollectionViewDatasource:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MessageItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
   
    cell.index = @(indexPath.item);
    
    return cell;
}



#pragma mark
#pragma mark 自定义滚动区 导入:
- (void)setupUI2
{
    //导航条滚动区:
    UIScrollView *navBar = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.navBar = navBar;

    [self.view addSubview:navBar];
    [navBar setShowsHorizontalScrollIndicator:NO];
    
    CGFloat totleW = 0;
    for(int i = 0;i < 2;i++)
    {
        CGFloat btnX = 0;
        if(i == 1)
        {
            btnX = [UIScreen mainScreen].bounds.size.width/2;
        }
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(btnX, 0, [UIScreen mainScreen].bounds.size.width /2, 50)];
        
#pragma mark
#pragma mark -设置button的字体颜色
        
//        
//        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.012 green:0.667 blue:0.668 alpha:1.000] forState:UIControlStateNormal];
        if (i == 1) {
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        
#pragma mark
#pragma mark - 
        
        
        self.btn = btn;
        
        [navBar addSubview:btn];
        if(i == 0)
        {
            [btn setTitle:@"就诊条件" forState:UIControlStateNormal];
            
        }else
        {
            [btn setTitle:@"医生简介" forState:UIControlStateNormal];
            
        }
        [btn setBackgroundColor:[UIColor whiteColor]];
        
        [btn setTag:i];
        [self.btnArray addObject:btn];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        totleW += btnX;
    }
    
    UIView *grayLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 35)];
    [navBar addSubview:grayLineView];
    [grayLineView setCenter:CGPointMake(navBar.bounds.size.width/2, navBar.bounds.size.height/2)];
    [grayLineView setBackgroundColor:[UIColor colorWithRed:0.708 green:0.719 blue:0.734 alpha:1.000]];
    
    //页数标识 rollView:
    UIView *rollView = [[UIView alloc]initWithFrame:CGRectMake([self.btnArray[0]bounds].size.width *0.2, 48, [self.btnArray[0]bounds].size.width *0.6, 2)];
    [rollView setBackgroundColor:[UIColor colorWithRed:0.278 green:0.829 blue:0.803 alpha:1.000]];
    [navBar addSubview:rollView];
    self.rollView = rollView;
    
    self.rollViewBeginX = self.rollView.center.x;
    self.rollViewBeginW = self.rollView.bounds.size.width;
    
    //下方咨询医生按钮:
    UIView *askView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.collectionView.frame), [UIScreen mainScreen].bounds.size.width, -124)];
    [self.view addSubview:askView];
    [askView setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMinY(askView.frame) +10, [UIScreen mainScreen].bounds.size.width -20, 40)];
    [self.view addSubview:button];
    [button setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"nav"]]];
    [button setTitle:@"咨询医生" forState:UIControlStateNormal];
    
}



//点击title列表按钮:
- (void)titleBtnClick:(UIButton *)btn
{

    if(self.isClick)
    {
       
        return;
    }
    
    if(btn.tag != self.currentPage)
    {
        self.isClick = YES;
        

    }
    
    self.currentPage = btn.tag;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.rollView setCenter:CGPointMake(btn.center.x, self.rollView.center.y)];
        [self.rollView setBounds:CGRectMake(0, 0, btn.bounds.size.width *0.6, 2)];
        
    }];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:btn.tag inSection:0] atScrollPosition:0 animated:YES];
    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isScroll = YES;
    
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if(self.isClick)
    {
        return;
    }
    
    
#pragma mark rollView移动:
    //判断滚动方向:
    CGFloat move = scrollView.contentOffset.x - self.scrollViewBeginX;
    //计算滚动比例:
    CGFloat scale =  move / [UIScreen mainScreen].bounds.size.width;
    
#pragma mark
#pragma mark -/////////////////
    
    
    
    
    //当前btn:
    UIButton *beginBtn = self.btnArray[self.currentPage];
    
    UIButton *nextBtn;
    //目标btn
    if(scale > 0 && self.currentPage < 1)
    {

        nextBtn = self.btnArray[self.currentPage + 1];
        //计算rollView需要移动的总距离:
        CGFloat rollMove = nextBtn.center.x - beginBtn.center.x;
        //移动
        self.rollView.center = CGPointMake(self.rollViewBeginX +rollMove * scale, self.rollView.center.y);
        
        
        //改变宽度:
        CGFloat widthChange = nextBtn.bounds.size.width - beginBtn.bounds.size.width;
        self.rollView.bounds = CGRectMake(0, 0, self.rollViewBeginW +widthChange *scale *0.6, self.rollView.bounds.size.height);
        
    }else if(scale < 0 && self.currentPage > 0)
    {
        nextBtn = self.btnArray[self.currentPage - 1];
        //计算rollView需要移动的总距离:
        CGFloat rollViewMove = nextBtn.center.x - beginBtn.center.x;
        //移动
        self.rollView.center = CGPointMake(self.rollViewBeginX -rollViewMove * scale, self.rollView.center.y);
        
    
        //改变宽度:
        CGFloat widthChange = nextBtn.bounds.size.width - beginBtn.bounds.size.width;
        self.rollView.bounds = CGRectMake(0, 0, self.rollViewBeginW -widthChange *scale *0.6, self.rollView.bounds.size.height);
    }
    
    
    
}

//代码滚动停止:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    self.isClick = NO;
    
    
}

//手动滚动停止:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //改变当前页码:
    self.currentPage = self.collectionView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
    
    self.scrollViewBeginX = self.collectionView.contentOffset.x;
    
    
    UIButton *currentBtn = self.btnArray[self.currentPage];
    self.rollView.bounds = CGRectMake(0, 0, currentBtn.bounds.size.width *0.6, self.rollView.bounds.size.height);
    self.rollView.center = CGPointMake(currentBtn.center.x, self.rollView.center.y);
    
    self.rollViewBeginX = self.rollView.center.x;
    self.rollViewBeginW = self.rollView.bounds.size.width;
    
    
}


@end













